<template>
  <view class="container">
    <z-paging-swiper>
      <view slot="top">
        <u-navbar height="50" title="订单管理" title-size="36" title-color="#333"></u-navbar>
        <view class="tabsView">
          <u-tabs-swiper
            ref="uTabs"
            :list="tabsList"
            :current="current"
            @change="tabsChange"
            :is-scroll="false"
            :show-bar="false"
            active-color="#0F76BC"
            swiper-width="750"
          >
          </u-tabs-swiper>
        </view>
        <u-gap height="20" bg-color="#F0F1F2"></u-gap>
      </view>
      <swiper
        class="swiperView"
        :current="scurrent"
        @transition="transition"
        @animationfinish="animationfinish"
      >
        <swiper-item v-for="(item2, index) in tabsList" :key="item2.id" class="swiperItem">
          <z-paging
            ref="paging"
            v-model="itemList[index]"
            @query="queryList"
            :fixed="true"
            :auto="false"
            :empty-view-img="`${getImageUrl('empty.png')} `"
            empty-view-text="暂无订单数据~"
            :empty-view-img-style="imgStyle"
          >
            <view class="goodsItemView">
              <view
                class="itemView"
                v-for="(item, index1) in itemList[index]"
                :key="index1"
                @click="onOrderDetail(item)"
              >
                <view class="order-top">
                  <view class=""> 交易单号：{{ item.order.no }} </view>
                  <view v-if="item.status == 0" class="order-top-tip"> 待处理 </view>
                  <view v-else-if="item.status == 10" class="order-top-tip"> 待发货 </view>
                  <view v-else-if="item.status == 11" class="order-top-tip"> 待收货 </view>
                  <view v-else-if="item.status == 12" class="order-top-tip"> 待退款 </view>
                  <view v-else-if="item.status == 5" class="order-top-tip"> 已完成 </view>
                </view>

                <view v-for="(item2, index2) in item.order.items" :key="index2" class="order-goods">
                  <u-icon class="goods-img" :name="item2.picUrl" size="160"></u-icon>
                  <view class="orde-riget">
                    <view class="goods-title">
                      {{ item2.spuName }}
                    </view>
                    <view class="goods-speci">
                      <text class="goods-num">x{{ item2.count }}</text>
                    </view>
                    <view class="goods-price"> ¥{{ (item2.price * 0.01).toFixed(2) }} </view>
                  </view>
                </view>
                <view class="price-total">
                  <view v-if="item.status != 4" class="">
                    共{{ item.order.items.length }}样商品 实际支付：¥{{
                      (item.order.payPrice * 0.01).toFixed(2)
                    }}
                    <p style="font-size: 24rpx">
                      （含运输费¥{{ (item.order.deliveryPrice * 0.01).toFixed(2) }}）
                    </p>
                  </view>
                  <p v-else>
                    共{{ item.order.items.length }}样商品 申请退款金额：<text style="color: #f54831"
                      >¥{{ (item.order.refundPrice * 0.01).toFixed(2) }}
                    </text>
                  </p>
                </view>
                <view class="btn-group" v-if="item.status == 0">
                  <view @click.stop.prevent="confirm(item)" class="solid-btn btn"> 立即接单 </view>
                  <view @click.stop.prevent="cancel(item)" class="hollow-btn btn"> 拒绝订单 </view>
                </view>
                <view class="btn-group" v-else-if="item.status == 10">
                  <view
                    @click.stop.prevent="
                      routeTo('/other/express/express?id=' + item.order.id + '&status=1')
                    "
                    class="solid-btn btn"
                  >
                    商家发货
                  </view>
                </view>
                <view class="btn-group" v-else-if="item.status == 11">
                  <!-- <view class="hollow-btn btn">
										修改物流
									</view> -->
                  <view class="hollow-btn btn"> 查看物流 </view>
                </view>
                <view class="btn-group" v-else-if="item.status == 12">
                  <view class="solid-btn btn"> 查看退款 </view>
                </view>
              </view>
            </view>
          </z-paging>
        </swiper-item>
      </swiper>
    </z-paging-swiper>
  </view>
</template>

<script>
export default {
  data() {
    return {
      imgStyle: {
        width: '12rem'
      },
      current: 0,
      scurrent: 0,
      tabsList: [
        {
          name: '全部',
          status: ''
        },
        {
          name: '待处理',
          status: 0
        },
        {
          name: '待发货',
          status: 10
        },
        {
          name: '待收货',
          status: 11
        },
        {
          name: '已完成',
          status: 14
        }
      ],
      itemList: [[], [], [], [], []],
      order_id: ''
    }
  },
  onLoad(res) {
    let self = this
    this.$nextTick(function () {
      self.current = Number(res.index) ? Number(res.index) : 0
      self.scurrent = Number(res.index) ? Number(res.index) : 0
      //self.onOrderLoad(self.current, null, true);
    })
    this.queryList()
  },
  onShow() {
    let self = this
    this.$nextTick(function () {
      //self.onOrderLoad(self.current, null, true);
    })
  },
  methods: {
    tabsChange(index) {
      this.scurrent = index
    },
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    animationfinish(e) {
      this.current = e.detail.current
      this.scurrent = e.detail.current
      this.$refs.uTabs.setFinishCurrent(e.detail.current)
      if (this.itemList[this.scurrent].length == 0) {
        this.queryList()
      }
    },
    queryList(pageNo = 1, pageSize = 10) {
      this.$u
        .get('/app-api/shop/trade-order/page', {
          status: this.tabsList[this.scurrent].status,
          pageNo,
          pageSize
        })
        .then((res) => {
          this.$refs.paging[this.scurrent].complete(res.data.list)
        })
    },

    confirm(e) {
      uni.dialog.confirm('温馨提示', '确认接单？', () => {
        this.$u.put('/app-api/shop/trade-order/accept', { id: e.id }).then(() => {
          this.$u.toast('成功')
          this.queryList()
        })
      })
    },

    cancel(e) {
      uni.dialog.confirm('温馨提示', '拒绝该订单？', () => {
        this.$u.put('/app-api/shop/trade-order/refuse', { id: e.id }).then(() => {
          this.$u.toast('成功')
          this.queryList()
        })
      })
    },
    onOrderCancel(item) {
      let self = this
      uni.dialog.confirm('温馨提示', '确定要取消订单吗？', () => {
        self.$u.api.setOrderCancel(item.id).then((res) => {
          self.$u.toast('取消成功')
          setTimeout(function () {
            self.onOrderLoad(self.current, null, true)
          }, 500)
        })
      })
    },
    onOrderPayment(item) {
      let self = this
      // self.$u.api.setTestPayments({
      // 	order_no: item.order_no,
      // 	openid: uni.getStorageSync('openid')
      // }).then(res => {
      // 	setTimeout(function() {
      // 		self.onOrderLoad(self.current, null, true);
      // 	}, 500);
      // });
      this.$utils.route({
        url: '/goods/payment/payment',
        params: {
          order_id: item.id,
          money: item.pay_price,
          total_weight: item.total_weight,
          delivery_price: item.delivery_price
        }
      })
    },
    onOrderConfrim(item) {
      let self = this
      uni.dialog.confirm('温馨提示', '该订单确认收货吗？', () => {
        self.$u.api.setOrderConfirm(item.id).then((res) => {
          self.$u.toast('收货成功')
          setTimeout(function () {
            self.onOrderLoad(self.current, null, true)
          }, 500)
        })
      })
    },
    onResellPush(item) {
      this.$utils.route({
        url: '/goods/resell-push/resell-push',
        params: {
          id: item.id
        }
      })
    },
    onPushComment(item) {
      this.$utils.route({
        url: '/other/push-comment/push-comment',
        params: {
          id: item.id
        }
      })
    },
    onOrderDetail(item) {
      //+ item.id + "&id=" +
      this.$utils.route({
        url: '/other_center/mer-order-detail/mer-order-detail?id=' + item.id
      })
    },
    routeTo(url) {
      uni.navigateTo({
        url
      })
    }
  }
}
</script>

<style>
@import url('../../goods/order-list/order-list.css');

page {
  height: 100%;
  background: #f0f1f2;
}
</style>
